package fragments;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;

import java.util.ArrayList;
import java.util.List;

import adapters.VersionAdapter;
import beans.GoodsDetailsBean;
import beans.HospitalGoodsInfo;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import netframe.GetRetrofit;
import netframe.NetAddress;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import utils.ToastUtil;


public class SelectHospitalFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.et_select_hospital_barcode)
    EditText etBarcode;
    @BindView(R.id.bt_select_hospital_barcode)
    Button btBarcode;
    @BindView(R.id.et_select_hospital_record)
    EditText etRecord;
    @BindView(R.id.bt_select_hospital_record)
    Button btRecord;
    @BindView(R.id.et_select_hospital_rfid)
    EditText etRfid;
    @BindView(R.id.bt_select_hospital_rfid)
    Button btRfid;
    @BindView(R.id.lv_select_hospital)
    ListView listview;
    @BindView(R.id.tv_select_hospital_count)
    TextView tvCount;
    @BindView(R.id.et_select_hospital_barcode_me)
    EditText etlBarcodeMe;
    @BindView(R.id.bt_select_hospital_barcode_me)
    Button btBarcodeMe;
    @BindView(R.id.et_select_hospital_rfid_me)
    EditText etRfidMe;
    @BindView(R.id.bt_select_hospital_rfid_me)
    Button btRfidMe;
    @BindView(R.id.tv_select_hospital_content)
    TextView tvSelectHospitalContent;
    private StringBuilder sb = new StringBuilder();
    private List<String> list = new ArrayList<>();
    private VersionAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_select_hospital, container, false);
        unbinder = ButterKnife.bind(this, inflate);

        return inflate;
    }

    @Override
    protected void initView() {
        super.initView();
        adapter = new VersionAdapter(mContext, list);
        listview.setAdapter(adapter);

    }

    @Override
    protected void setListener() {
        super.setListener();

        btBarcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String barCode = etBarcode.getText().toString().trim();
                if (!TextUtils.isEmpty(barCode)) {
                    getGoodsInfo(barCode, 1);

                } else {
                    ToastUtil.showToast(mContext, "请输入条码!");
                }

            }
        });

        btRfid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rfid = etRfid.getText().toString().trim();
                if (!TextUtils.isEmpty(rfid)) {
                    getGoodsInfo(rfid, 3);

                } else {
                    ToastUtil.showToast(mContext, "请输入 RFID !");
                }
            }
        });

        btRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String record = etRecord.getText().toString().trim();
                if (!TextUtils.isEmpty(record)) {
                    getGoodsInfo(record, 2);

                } else {
                    ToastUtil.showToast(mContext, "请输入记录号!");
                }
            }
        });

        btBarcodeMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String barCode = etlBarcodeMe.getText().toString().trim();
                if (!TextUtils.isEmpty(barCode)) {
                    getGoodsInfoMe("select * from GoodsDetails where BarCode = '" + barCode + "'");

                } else {
                    ToastUtil.showToast(mContext, "请输入条码!");
                }
            }
        });

        btRfidMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rfid = etRfidMe.getText().toString().trim();
                if (!TextUtils.isEmpty(rfid)) {
                    getGoodsInfoMe("select * from GoodsDetails where RfidCode = '" + rfid + "'");
                } else {
                    ToastUtil.showToast(mContext, "请输入 RFID !");
                }
            }
        });
    }

    /**
     * 查询医院数据库
     *
     * @param data rfid , 记录号 , 条码
     * @param type 1 条码 ，2 记录号 ， 3 rfid
     */
    private void getGoodsInfo(String data, int type) {
        list.clear();
        GetRetrofit.getInstance2().selectToHospital(data, type)
                .enqueue(new Callback<List<HospitalGoodsInfo>>() {
                    @Override
                    public void onResponse(Call<List<HospitalGoodsInfo>> call, Response<List<HospitalGoodsInfo>> response) {
                        List<HospitalGoodsInfo> body = response.body();
                        if (body != null && body.size() != 0) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < body.size(); i++) {
                                HospitalGoodsInfo hospitalGoodsInfo = body.get(i);
                                sb.append(hospitalGoodsInfo.toString());
                                sb.append("\r\n");
                                sb.append("-----------------------------------------");
                                sb.append("\r\n");
                            }
                            tvSelectHospitalContent.setText(sb.toString());
                            tvCount.setVisibility(View.VISIBLE);
                            tvCount.setText("查询到 " + body.size() + " 条数据");
                        } else {
                            tvCount.setVisibility(View.GONE);
                            list.add("没有查询到数据");
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<List<HospitalGoodsInfo>> call, Throwable t) {
                        list.add("网络请求失败!");
                        tvCount.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    /**
     * 查询我们自己的数据库
     *
     * @param sql
     */

    private void getGoodsInfoMe(String sql) {
        list.clear();
        GetRetrofit.getInstance().getGoodsDetailsPage(NetAddress.token, NetAddress.machineCode, sql)
                .enqueue(new Callback<ArrayList<GoodsDetailsBean>>() {
                    @Override
                    public void onResponse(Call<ArrayList<GoodsDetailsBean>> call, Response<ArrayList<GoodsDetailsBean>> response) {
                        ArrayList<GoodsDetailsBean> body = response.body();
                        if (body != null && body.size() != 0) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < body.size(); i++) {
                                GoodsDetailsBean goodsDetailsBean = body.get(i);
//                                list.add(goodsDetailsBean.toString());
                                sb.append(goodsDetailsBean.toString());
                                sb.append("\r\n");
                                sb.append("-----------------------------------------");
                                sb.append("\r\n");
                            }
                            tvSelectHospitalContent.setText(sb.toString());
                            tvCount.setVisibility(View.VISIBLE);
                            tvCount.setText("查询到 " + body.size() + " 条数据");

                        } else {
                            tvCount.setVisibility(View.GONE);
                            list.add("没有查询到数据");
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<ArrayList<GoodsDetailsBean>> call, Throwable t) {
                        list.add("网络请求失败!");
                        tvCount.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
